<?php
class FileuploadsController extends AppController {
	//Khai bao name cua controller, ten controller phai tuong ung voi ten cua view.
	// luu y: "$this->params['controller']" duoc su dung de lay ten cua controller khi viet code o cac trang view
	public $name = 'Fileuploads';
	//khai bao cac model can su dung trong controller
	//luu y: "$this->uses[0]" duoc su dung de lay ten cua cac model khi viet code o controller va`
	//        "$this->params['models'][0]" duoc su dung de lay ten cua model khi viet code o cac trang view
	public $uses=array('FileManager');
	//khai bao cac helper de su dung khi viet code o view, lu y chi can khai bao ten, ko can Helper o phia sau, vd CommonHelper -> Common
	//cac helper mac dinh da co la SessionHelper, FormHelper, HtmlHelper....
	//khi goi cac helper o ngoai view thi goi nhu sau:  SessionHelper -> $this->Session, FormHelper -> $form, CommonHelper-> $this->Common
	public $components=array('ImageUtil','Common');
	
	function beforeFilter(){
		
		 if (isset($this->data['PHPSESSID'])) {
            $this->Session->id($this->data['PHPSESSID']);
           // $this->Session->start();            
        }       
        
        parent::beforeFilter();
	}
	
	function admin_imageupload($type=null,$model=null,$galleryId=null){	

		if ($this->params['form']['Filedata']['error'] == 0 && $type!=null){
			$fileName = $this->params['form']['Filedata']['name'];
			$filePath='';
			//wipe out .jpg.jpg			
			
			//$fileName=str_replace(' ','_',$fileName);
				
			
				
				if($type=="document"){
					$fileName=substr($this->params['form']['Filedata']['name'],0, strrpos($fileName,'.')).substr($this->params['form']['Filedata']['name'], strrpos($fileName,'.'));
					//check file exist
					$data=$this->FileManager->find('first',array('conditions'=>array('model'=>$model,'name'=>$fileName)));
					if(!empty($data)){
						if($data['FileManager']['mount']>0){
							echo "ERROR: file <b>".$fileName.'</b> existed.';
							exit;
						}
						else{
							$this->FileManager->deleteAll(array('name'=>$data['FileManager']['name']));
						}
					}
					$this->Common->createWebRootFolder('uploaded/'.$model.'/document/');	
					$uploadImage=WWW_ROOT.'uploaded/'.$model.'/document/'.$fileName;
					//save file		
					$this->Common->createWebRootFolder('uploaded/'.$model.'/document/');		
					move_uploaded_file($this->params['form']['Filedata']['tmp_name'], $uploadImage);
					$filePath= SITE_PATH.'/uploaded/'.$model.'/document/'.$fileName;
				}
				else{
					$fileName=substr($this->params['form']['Filedata']['name'],0, strrpos($fileName,'.')).'-'.time().substr($this->params['form']['Filedata']['name'], strrpos($fileName,'.'));
					
					$this->Common->createWebRootFolder('uploaded/'.$model.'/image/');				
					$uploadImage=WWW_ROOT.'uploaded/'.$model.'/image/'.$fileName;
					//save image			
					move_uploaded_file($this->params['form']['Filedata']['tmp_name'], $uploadImage);
					
					//large image	
					$this->Common->createWebRootFolder('uploaded/'.$model.'/large/');		
					$Image=WWW_ROOT.'uploaded/'.$model.'/large/'.$fileName;
					$height=0;				
					$width=600;				
					//get size config
					if($thumbConfigSize=Configure::read($model.'.large')){
						$height=$thumbConfigSize['height'];				
						$width=$thumbConfigSize['width'];;
					}
					if($height && $width)$this->ImageUtil->convertImageSize($uploadImage,$Image,$width,$height);
					
					//thumb image (thumb1)
					$this->Common->createWebRootFolder('uploaded/'.$model.'/thumb/');	
					$Image=WWW_ROOT.'uploaded/'.$model.'/thumb/'.$fileName;
					$height=0;
					$width=120;
					//get size config
					if($thumbConfigSize=Configure::read($model.'.thumb')){
						$height=$thumbConfigSize['height'];				
						$width=$thumbConfigSize['width'];;
					}
					if($height && $width)$this->ImageUtil->convertImageSize($uploadImage,$Image,$width,$height);
					//thumb2 image (thumb2) ... the same as thumb1 but change the size ($thumbwidth and $thumbheight)
					$filePath=SITE_PATH.'/uploaded/'.$model.'/thumb/'.$fileName;	
					
				}
				
				//save to db				
				$this->FileManager->save(array('FileManager'=>array('model'=>$model,'type'=>$type,'name'=>$fileName)));
				echo "FILEID:".$this->FileManager->getLastInsertID().":FILEPATH:" . $filePath;	// Return the file id to the script
			
		}
		exit;
		/*
		$f=fopen(WWW_ROOT.'files/test.txt','w');
		
		ob_start();
		//t($this,0);
		$data=ob_get_contents();
		
		fwrite($f,ob_get_contents());
		ob_end_clean();
		fclose($f);
		exit;
		*/
		//$this->render('admin_index','ajax');
	}	
	

	

}
?>
